package cn.tedu.egaddress1.controller;

import cn.tedu.egaddress1.mapper.AddressMapper;
import cn.tedu.egaddress1.pojo.dto.AddressAddParam;
import cn.tedu.egaddress1.pojo.entity.Address;
import cn.tedu.egaddress1.pojo.vo.AddressInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/address")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("/add")
    public String add(@RequestBody AddressAddParam addressAddParam) {
        System.out.println("addressAddParam" + addressAddParam);

        Address address = new Address();

        BeanUtils.copyProperties(addressAddParam, address);

        int i = addressMapper.insert(address);

        if (i > 0) {
            return "success";
        }else {
            return "fail";
        }

    }

    @GetMapping("/list")
    public List<AddressInfoVo> list() {

        List<AddressInfoVo> list = addressMapper.selectList();

        return list;

    }

    @PostMapping("/del/{addressId}")
    public String del(@PathVariable("addressId") Integer addressId) {
        System.out.println("addressId" + addressId);

        int i = addressMapper.delById(addressId);
        if (i > 0) {
            return "success";
        }else{
            return "fail";
        }
    }

}
